package com.github.authcloud.server.configure;

import com.github.authcloud.common.AuthConstant;
import com.github.authcloud.common.RequiresRolePermissionMapping;
import com.github.authcloud.common.NoRequiresLoginMapping;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author : zhaoxuan
 * @description : AuthRolePermissionLocalCache
 */
public class AuthRolePermissionLocalCache {

    private static final ConcurrentHashMap<String, List<NoRequiresLoginMapping>> NO_LOGIN_MAPPING_MAP = new ConcurrentHashMap<>();
    private static final ConcurrentHashMap<String, List<RequiresRolePermissionMapping>> AUTH_MAPPING_MAP = new ConcurrentHashMap<>();


    public static void syncNoLoginMappingMap(String serviceName, RedisTemplate redisTemplate){
        List<NoRequiresLoginMapping> noLoginInfos = (List<NoRequiresLoginMapping>)redisTemplate.opsForHash().get(AuthConstant.NO_LOGIN_MAPPING_LISTENER_KEY, serviceName);
        NO_LOGIN_MAPPING_MAP.put(serviceName, noLoginInfos != null ? noLoginInfos : new ArrayList<>());
    }

    public static void syncRolePermissionMappingMap(String serviceName, RedisTemplate redisTemplate){
        List<RequiresRolePermissionMapping> authInfos = (List<RequiresRolePermissionMapping>)redisTemplate.opsForHash().get(AuthConstant.AUTH_MAPPING_LISTENER_KEY, serviceName);
        AUTH_MAPPING_MAP.put(serviceName, authInfos != null ? authInfos : new ArrayList<>());
    }

    public static List<NoRequiresLoginMapping> getNoLoginMapping(String serviceName){
        serviceName = serviceName.toUpperCase();
        return NO_LOGIN_MAPPING_MAP.get(serviceName);
    }

    public static List<RequiresRolePermissionMapping> getRolesPermissionMapping(String serviceName){
        serviceName = serviceName.toUpperCase();
        return AUTH_MAPPING_MAP.get(serviceName);
    }

    public static void syncNoLoginMappingMapInit(RedisTemplate<String, String> redisTemplate) {
        Set<Object> keys = redisTemplate.opsForHash().keys(AuthConstant.NO_LOGIN_MAPPING_LISTENER_KEY);
        for (Object keyOb : keys) {
            List<NoRequiresLoginMapping> noLoginInfos = (List<NoRequiresLoginMapping>)redisTemplate.opsForHash().get(AuthConstant.NO_LOGIN_MAPPING_LISTENER_KEY, keyOb);
            NO_LOGIN_MAPPING_MAP.put(keyOb.toString(),  noLoginInfos != null ? noLoginInfos : new ArrayList<>());
        }
    }

    public static void syncRolePermissionMappingMapInit(RedisTemplate<String, String> redisTemplate) {
        Set<Object> keys = redisTemplate.opsForHash().keys(AuthConstant.AUTH_MAPPING_LISTENER_KEY);
        for (Object keyOb : keys) {
            List<RequiresRolePermissionMapping> authInfos = (List<RequiresRolePermissionMapping>)redisTemplate.opsForHash().get(AuthConstant.AUTH_MAPPING_LISTENER_KEY, keyOb);
            AUTH_MAPPING_MAP.put(keyOb.toString(),  authInfos != null ? authInfos : new ArrayList<>());
        }
    }
}
